package com.kingschan.jeasytag.sql.parse.fieldtype;

import org.json.JSONObject;

import com.kingschan.jeasytag.sql.parse.AbstractAnalysis;
import com.kingschan.util.lang.StringUtil;
/**
 * TextAnalysis
 * @author kingschan
 *date:2013-4-24
 */
public class TextAnalysis extends AbstractAnalysis{
	/**
	 * generic
	 * 
	 * 
	 * 
	 * 
	 * 
	 */
	
	
	private String generic(JSONObject filter) throws Exception{
		if ("".equals(StringUtil.ConvertNullStr(filter.getString("value")))) {
			return "";
		}		
		StringBuffer sb = new StringBuffer();
		sb.append(filter.getString("filterfield")).append(" ");
		if (filter.getString("Operator").equals("Contain")) {
			sb.append("like '%").append(filter.getString("value")).append("%' ");
		}else if(filter.getString("Operator").equals("likeLeft")){
			sb.append("like '").append(filter.getString("value")).append("%' ");
		}else if(filter.getString("Operator").equals("likeRight")){
			sb.append("like '%").append(filter.getString("value")).append("' ");
		}
		else if(filter.getString("Operator").equals("in")){
			sb.append(" in (").append(StringUtil.convertStrToSqlInstr(filter.getString("value"), false)).append(")");
		}else{
			sb.append(AbstractAnalysis.Operator.get(filter.getString("Operator"))).append("'").append(filter.getString("value")).append("'");
		}
		return sb.toString();
		
	}
	
	
	@Override
	protected String mysqlAnalysis(JSONObject filter) throws Exception {		
		return this.generic(filter);
	}

	@Override
	protected String oracleAnalysis(JSONObject filter) throws Exception {		
		return this.generic(filter);
	}

	@Override
	protected String sqlserverAnalysis(JSONObject filter) throws Exception {		
		return this.generic(filter);
	}


}
